package com.smallA.service.Impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.smallA.dao.QuestionMapper;
import com.smallA.po.ExcelTest;
import com.smallA.service.QuestionService;
import com.smallA.utils.ExcelUtils;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

/**
 * @author 君未洋
 * 实现类
 */
public class QuestionServiceImpl implements QuestionService {
    private QuestionMapper questionMapper;

    public void setQuestionMapper(QuestionMapper questionMapper) {
        this.questionMapper = questionMapper;
    }

    @Override
    public boolean insertQuestion(String path, CommonsMultipartFile file) {
        try {
            File realPath = new File(path);
            if (!realPath.exists()) {
                realPath.mkdir();
            }
            String filePath = realPath + "/" + file.getOriginalFilename();
            System.out.println(filePath);
            //获取文件保存的路径
            file.transferTo(new File(filePath));
            ExcelReader excelReader = null;
            try {
                excelReader = EasyExcel.read(filePath, ExcelTest.class, new ExcelUtils(questionMapper)).build();
                ReadSheet readSheet = EasyExcel.readSheet(0).build();
                excelReader.read(readSheet);
                return true;
                //通过CommonsMultipartFile的方法直接写文件
            } finally {
                if (excelReader != null) {
                    // 这里千万别忘记关闭，读的时候会创建临时文件，到时磁盘会崩的
                    excelReader.finish();
                }
            }
        } catch (IOException e) {
            return false;
        }
    }

    @Override
    public HttpServletResponse dowmloadTemplate(HttpServletResponse resp, HttpServletRequest req) {
        String path = req.getServletContext().getRealPath("/static/template");
        //要下载的文件地址
        String fileName = "试题模板.xlsx";
        //对应的具体文件名
        //1、设置response 响应头
        resp.reset();
        //设置页面不缓存,清空buffer
        resp.setCharacterEncoding("UTF-8");
        //字符编码
        resp.setContentType("multipart/form-data");
        //二进制传输数据

        try {
            resp.setHeader("Content-Disposition",
                    "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
            //设置响应头
            File file = new File(path, fileName);
            //2、 读取文件--输入流
            InputStream input = new FileInputStream(file);
            //3、 写出文件--输出流
            OutputStream out = resp.getOutputStream();
            byte[] buff = new byte[1024];
            int index = 0;
            //4、执行 写出操作
            while ((index = input.read(buff)) != -1) {
                out.write(buff, 0, index);
                out.flush();
            }
            out.close();
            input.close();
        } catch (Exception e) {
            return resp;
        }
        return resp;
    }
}
